package com.sxx.mall.coupon.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxx.commons.dto.SkuReductionDTO;
import com.sxx.commons.utils.PageUtils;
import com.sxx.commons.utils.Query;
import com.sxx.mall.coupon.dao.SkuFullReductionDao;
import com.sxx.mall.coupon.entity.MemberPriceEntity;
import com.sxx.mall.coupon.entity.SkuFullReductionEntity;
import com.sxx.mall.coupon.entity.SkuLadderEntity;
import com.sxx.mall.coupon.service.MemberPriceService;
import com.sxx.mall.coupon.service.SkuFullReductionService;
import com.sxx.mall.coupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.ws.Action;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Autowired
    SkuFullReductionService skuFullReductionService;

    @Autowired
    SkuLadderService skuLadderService;

    @Autowired
    MemberPriceService memberPriceService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    /*
    * 保存满减、折扣、会员价相关信息
    * */
    @Override
    @Transactional
    public void saveSkuReduction(SkuReductionDTO dto) {
      //  5.4保存mall-sms下sms_sku_ladder折扣信息，sms_sku_full_reduction满减信息，sms_member_price会员价信息
        //满减信息
        //满减大于0
        if (dto.getFullPrice()!= null &&
                dto.getFullPrice().compareTo(new BigDecimal(0)) == 1){
            SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
            BeanUtils.copyProperties(dto,skuFullReductionEntity);
            skuFullReductionEntity.setAddOther(dto.getPriceStatus());
            skuFullReductionService.save(skuFullReductionEntity);
        }

        //折扣信息
        if (dto.getFullCount() > 0 ){
            SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
            BeanUtils.copyProperties(dto,skuLadderEntity);
            skuLadderEntity.setAddOther(dto.getCountStatus());
            skuLadderService.save(skuLadderEntity);
        }
        // 会员价信息
        if (dto.getMemberPrice() != null){
            List<MemberPriceEntity> memberPriceEntities = dto.getMemberPrice().stream().map(memberPrice -> {
                MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
                if (memberPrice.getPrice() != null){
                    memberPriceEntity.setSkuId(dto.getSkuId());
                    memberPriceEntity.setMemberLevelId(memberPrice.getId());
                    memberPriceEntity.setMemberLevelName(memberPrice.getName());
                    memberPriceEntity.setMemberPrice(memberPrice.getPrice());
                    memberPriceEntity.setAddOther(1);//是否可叠加优惠
                }
                return memberPriceEntity;
            }).collect(Collectors.toList());
            memberPriceService.saveBatch(memberPriceEntities);

        }

    }

}